//
//  LS_DestinalProtocolImp.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/5.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_DestinalProtocolImp.h"
#import "LS_DestinationModel.h"
#import "LS_DestinVideoModel.h"
@interface LS_DestinalProtocolImp()

/**
 feed数据
 */
@property(nonatomic,strong)NSMutableArray *feedsData;

/**
 video数据
 */
@property(nonatomic,strong)NSMutableArray *videoData;

/**
 所有数据
 */
@property(nonatomic,strong)NSMutableDictionary  *destinData;
@end
@implementation LS_DestinalProtocolImp
-(RACSignal *)requestDestineSingnal:(NSString *)requestUrl
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSString *lng = [CommonUtils getStrValueInUDWithKey:Longitudekey];
        NSString *lat = [CommonUtils getStrValueInUDWithKey:Latiudekey];
        
        NSDictionary *params = @{
                                 @"count" : @"20",
                                 @"lat"   : lat ? lat : @"22.53512980326447",
                                 @"lng"   : lng ? lng : @"114.0598045463555",
                                 @"start" : @"0",
                                 @"video_count" : @"2"
                                 };
        HTURLSessionTask *task = [HTNetWorking getWithUrl:requestUrl refreshCache:YES showHUD:@"loading..." params:params success:^(id response) {
            [self.feedsData removeAllObjects];
            [self.videoData removeAllObjects];
            
            NSArray *feeds = response[@"data"][@"feeds"];
            NSArray *vides = response[@"data"][@"videos"];
            //feed数据
            [feeds enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj[@"product"] count] > 0) {
                    LS_DestinationModel *feedModel = [LS_DestinationModel mj_objectWithKeyValues:obj];
                    [self.feedsData addObject:feedModel];
                }
            }];
            //video数据
            [vides enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                LS_DestinVideoModel *model = [LS_DestinVideoModel mj_objectWithKeyValues:obj];
                [self.videoData addObject:model];
            }];
            
            //创造数据上传
            [self.destinData setObject:self.feedsData forKey:DestinDataKey];
            [self.destinData setObject:self.videoData forKey:DestinVideoKey];
            
            [subscriber sendNext:self.destinData];
            [subscriber sendCompleted];
        } fail:^(NSError *error) {
            [subscriber sendNext:error];
        }];
        
        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
    }];
}
-(NSMutableArray *)videoData
{
    return LS_LAZY(_videoData, @[].mutableCopy);
}
-(NSMutableArray *)feedsData
{
    return LS_LAZY(_feedsData, @[].mutableCopy);
}
-(NSMutableDictionary *)destinData
{
    return LS_LAZY(_destinData, @{}.mutableCopy);
}
@end
